package api;

import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.CreateTableOptions;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;

import java.util.ArrayList;

public class CreateTableDemo {
    public static void main(String[] args) {

        String masterAddress = "linux122";
        KuduClient.KuduClientBuilder kuduClientBuilder = new KuduClient.KuduClientBuilder(masterAddress);
        KuduClient client = kuduClientBuilder.build();
        String tableName = "school";

        ArrayList<ColumnSchema> columnSchemas = new ArrayList<ColumnSchema>();
        ColumnSchema id = new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build();
        ColumnSchema name = new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).build();
        ColumnSchema createTime = new ColumnSchema.ColumnSchemaBuilder("create_time", Type.INT32).key(false).build();
        columnSchemas.add(id);
        columnSchemas.add(name);
        columnSchemas.add(createTime);
        Schema schema = new Schema(columnSchemas);

        CreateTableOptions options = new CreateTableOptions();

        options.setNumReplicas(1);
        ArrayList<String> colrule = new ArrayList<String>();
        colrule.add("id");
        options.addHashPartitions(colrule, 3);

        try {
            client.createTable(tableName, schema, options);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }
}
